package com.game.sprite;

import org.jbox2d.collision.shapes.CircleShape;
import org.jbox2d.collision.shapes.PolygonShape;
import org.jbox2d.collision.shapes.ShapeType;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.jbox2d.dynamics.World;

import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;

import com.game.utils.JBox2dUtil;


/**
* @author oven:
* @version 创建时间：2009-1-1 上午7:43:51
* 
*/
public abstract class BasicPhysicalSprite extends BasicSprite{
	public World world;
	public Body phySicalbody;
	public Vec2 position;
	//由于绘图的参考点(左上角)与box2d的参考点(中心)不一样,因此需要调整
	//精灵在屏幕上的宽高
	public float angle;
	
	public BasicPhysicalSprite(World world){
		this.world= world;
	}
	
	public void updatePost(){
		position = phySicalbody.getPosition();
		setPostX(position.x*JBox2dUtil.RATE-getHeight()/2);
		setPostY(position.y*JBox2dUtil.RATE-getWidth()/2);
	}
	
	public void draw(Canvas canvas){
		updatePost();
		//保存画布原来的状态,旋转画布
		canvas.save();
		angle=(float)(phySicalbody.getAngle() * 180 / Math.PI);
		canvas.rotate(angle,position.x * JBox2dUtil.RATE, position.y * JBox2dUtil.RATE);
		super.draw(canvas);
//		paint(canvas);
		drawShape(phySicalbody,canvas);
		
		//恢复画布
		canvas.restore();
	}
	
	/**
	 * 获取世界坐标
	 * @return
	 */
	public Vec2 getWorldCenter(){
		return phySicalbody.getWorldCenter();
	}
	
	/**
	 * 绘制形状
	 * @param body
	 * @param canvas
	 */
	public void drawShape(Body body,Canvas canvas){
		//保存画笔原来的状态
		int color = paint.getColor();
		Style style = paint.getStyle();
		paint.setColor(Color.WHITE);
		paint.setStyle(Paint.Style.STROKE);
		
		Vec2 position = body.getPosition();
		//判断是否多边形
		if(body.getFixtureList().m_shape.m_type!=ShapeType.POLYGON){
			CircleShape shape = (CircleShape)(body.getFixtureList().m_shape);
			float radius=shape.m_radius*JBox2dUtil.RATE;
			float pointx=JBox2dUtil.RATE*position.x;
			float pointy=JBox2dUtil.RATE*position.y;
			canvas.drawCircle(pointx, pointy, radius, paint);
			//两种方式绘制圆形
//			canvas.drawCircle(postX+width/2, postY+height/2, width/2, paint);
			
		}else{
			PolygonShape shape = (PolygonShape)(body.getFixtureList().m_shape);
			//获取所有顶点
			Vec2[] vertexs=shape.m_vertices;
			int vertexCount = shape.m_vertexCount;
			for(int i=0;i<vertexCount;i++){
				Vec2 vertexs1;
				Vec2 vertexs2;
				if(i==vertexCount-1){
					//1~3顶点
					vertexs1= vertexs[i];
					vertexs2= vertexs[0];
				}else{
					//
					vertexs1= vertexs[i];
					vertexs2= vertexs[i+1];
				}
				float point1x=JBox2dUtil.RATE*(position.x+vertexs1.x);
				float point1y=JBox2dUtil.RATE*(position.y+vertexs1.y);
				float point2x=JBox2dUtil.RATE*(position.x+vertexs2.x);
				float point2y=JBox2dUtil.RATE*(position.y+vertexs2.y);
				canvas.drawLine(point1x, point1y, point2x, point2y,paint);
			}
		}
		//还原画笔
		paint.setStyle(style);
		paint.setColor(color);
	}
	
	/**
	 * 为phySicalbody添加一个作用力
	 * @param force
	 * @param point
	 */
	public void applyForce(Vec2 force){
		phySicalbody.applyForce(force,phySicalbody.getWorldCenter());
	}
	
	/**
	 * 设置sprite的宽高
	 * @param body
	 */
	public void setWidthHeightWithVertexs(Body body){
		if(body.getFixtureList().m_shape.m_type!=ShapeType.POLYGON){
			CircleShape shape = (CircleShape)(body.getFixtureList().m_shape);
			float radius=shape.m_radius*JBox2dUtil.RATE;
			setWidth(radius*2);
			setHeight(radius*2);
			return;
		}
		PolygonShape shape = (PolygonShape)(body.getFixtureList().m_shape);
		Vec2[] vertexs=shape.m_vertices;
		setWidthHeightWidthVertexs(vertexs);
	}
	
	public void setWidthHeightWidthVertexs(Vec2[] vertexs){
		float[] values = JBox2dUtil.getWidthHeight(vertexs);
		setWidth(values[0]);
		setHeight(values[1]);
	}
}
